import { Component, OnInit } from '@angular/core';
import { PropertyService } from '../service/property.service';
import { Router, ActivatedRoute } from '@angular/router';
import { Message } from 'primeng/api';
import { CRUD_MODE } from '../../shared/config/app-settings';
import * as _ from 'lodash';
import { TranslateService } from '@ngx-translate/core';
import { MessageUtilsService } from '../../shared/services/message-utils.service';
@Component({
  selector: 'property-grid',
  templateUrl: './property-grid.component.html',
  styleUrls: ['./property-grid.component.css']
})
export class PropertyGridComponent implements OnInit {
  msgs: Message[] = [];
  //p-table
  selectedColumns: any[];
  dataSource: any;
  cols: any[];
  constructor(private route: ActivatedRoute,
    private router: Router,
    private propertyService: PropertyService,
    private translateService:TranslateService,
    private messageUtilsService:MessageUtilsService) { }

  ngOnInit() {
    //init table
    this.cols = [
      { field: 'id', header: this.translateService.instant('ID') },
      { field: 'projectName', header: this.translateService.instant('PROJECT') },
      { field: 'lot', header: this.translateService.instant('LOT#') },
      { field: 'type', header: this.translateService.instant('TYPE') },
      { field: 'agent_code', header: this.translateService.instant('AGENT') },
      { field: 'owner_code', header: this.translateService.instant('OWNER') },
      { field: 'sale_status', header: this.translateService.instant('STATUS') }
    ];
    this.selectedColumns = _.cloneDeep(this.cols); //toggle cols
    this.selectedColumns.splice(0,1); //default hide first column
    this.loadAllProperties();
  }
  //ui-event
  onReload() {
    this.loadAllProperties();
  }
  onAddEntity() {
    this.router.navigate(['../property-detail/0'], { relativeTo: this.route, queryParams: { menuKey: 'house-detail', mode: CRUD_MODE.CREATE } });
  }

  onEditRow(rowData) {
    this.router.navigate(['../property-detail/' + rowData.id], { relativeTo: this.route, queryParams: { menuKey: 'house-detail', mode: CRUD_MODE.EDIT } });
  }

  onViewRow(rowData) {
    this.router.navigate(['../property-detail/' + rowData.id], { relativeTo: this.route, queryParams: { menuKey: 'house-detail', mode: CRUD_MODE.VIEW } });
  }

  onDeleteRow(rowData){
    this.propertyService.delete([rowData.id]).subscribe(
      (res)=>{
        this.messageUtilsService.showInfo(this.msgs, "Delete success");
        this.loadAllProperties();
      }
    );
  }



  //server exchange
  loadAllProperties() {
    this.propertyService.getAllProperties().subscribe(
      (res) => {
        this.dataSource = res;
        _.each(this.dataSource, (item) => {
          item.agent_code = item.agent ? item.agent.code : '';
          item.owner_code = item.owner ? item.owner.code : '';
          item.propertylist_id = item.propertyList ? item.propertyList.id : '';
          item.projectName = item.propertyList ? item.propertyList.name : '';
        });

      },
      (err) => {
        this.messageUtilsService.showError(this.msgs, err);

      }
    )
  }

}
